<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LiebiaoController extends Controller
{
    public function index($id){


        $did = DB::table('gonggao')->get();
        $dif = DB::table('link')->get();
        // dd($did);
        //判断是几级类
        $path = DB::table('cates')->select('path')->where('id',$id)->first();
        // dd($path); 
        $tmp = explode(',',$path->path);
        $len = count($tmp);
        switch($len){
            //一级类
            case 1:
                //二级类的id
                $erji = DB::table('cates')->select('id')->where('pid',$id)->get();
                // dd($erji);
                $data = [];
                foreach($erji as $k=>$v){
                    // 三级类的id
                    $sanji = DB::table('cates')->select('id')->where('pid',$v->id)->get();
                    // 将三级类的id压入数组
                    foreach($sanji as $kk=>$vv){
                        $data[] = $vv->id;
                    }  
                }
                // dd($data);
                // //根据三级类的id查询商品
                // $goods = [];
                // foreach($data as $k=>$v){
                //     $tmp = DB::table('goods')->where('id',$v)->first();
                //     $goods[] = $tmp;
                // }
                // dd($goods);
                break;
            //二级类
            case 2:
                //三级类的id
                $sanji = DB::table('cates')->select('id')->where('pid',$id)->get();
                // dd($sanji);
                $goods = [];
                foreach($sanji as $k=>$v){
                    $data[] = $v->id;
                }
                // dd($data);
                break;
            //三级类
            case 3:
                 
                $data[] = $id; 
                break;

            default: return back();
        }
        //根据三级类的id查询商品
        $goods = [];
        foreach($data as $k=>$v){
            $tmp = DB::table('goods')->where('cateid',$v)->get();
            foreach($tmp as $kk=>$vv){
                $goods[] = $vv;
                
            }
            // dd($tmp);
        }
        // dd($goods);

           
       
        // dd($len);
       
        return view('goods.liebiao',['goods'=>$goods,'aaa'=>$did,'lianjie'=>$dif]);
        }

        public function search(Request $request)
        {   
            $did = DB::table('gonggao')->get();
            $dif = DB::table('link')->get();
            // dd($request->input('keywords')  );
            // 判断是否有keywords搜索条件
            if(empty($request->input('keywords'))){
                //查询数据
                $goods = DB::table('goods')
                ->paginate(20);
            }else{
                //查询数据
                $goods = DB::table('goods')
                ->where('proname','like','%'.$request->input("keywords").'%')
                ->paginate(20);
            }
            //分配商品列表模板 分配变量
            return view('goods.liebiao',['goods'=>$goods,'aaa'=>$did,'lianjie'=>$dif]);
        }
   }

